/*
============================================================================
Name        : CVersificatorContainer from VersificatorContainer.h
Author      : 
Version     :
Copyright   : Your copyright notice
Description : Container control implementation
============================================================================
*/

// INCLUDE FILES
#include "VersificatorContainer.h"
#include "VersificatorAppUi.h"
#include "PluginInterface.h"
#include "PluginUtility.h"
#include "Ticker.h"

#include <aknutils.h>	//Fonts

#define MICRO_SECONDS_MAX_REPAINT 500

CVersificatorContainer::CVersificatorContainer(CVersificatorAppUi& aAppUi)
: iAppUi(aAppUi), iIconBitmap(NULL), iIconBitmapMask(NULL), iTextFont(NULL), iTitleFont(NULL),
  iTextFontHeight(0), iTitleFontHeight(0), iTitle(NULL), iTitleWidth(0)
{
}

// Destructor
CVersificatorContainer::~CVersificatorContainer()
{
	//Delete objects
	DELETE(iTicker);
	DELETE(iTitle);
}

void CVersificatorContainer::ConstructL(const TRect& aRect)
{
	//Set fonts
	iTextFont = CONST_CAST(CFont*, iCoeEnv->NormalFont());
	iTitleFont = CONST_CAST(CFont*, LatinBold17());

	//Font heights
	iTextFontHeight = iTextFont->HeightInPixels();
	iTitleFontHeight = iTitleFont->HeightInPixels();

	//Ticker width
	iTickerWidth = aRect.Size().iWidth - 33;

	//Initialize draw control
	iLastDrawTime.HomeTime();

	//Create window
	CreateWindowL();

	//Controls
	CreateControlsL();

	//Set rectangle
	SetRect(aRect);

	//Activate control
	ActivateL();
}

void CVersificatorContainer::CreateControlsL()
{
	iTicker = CTicker::NewL(this);
}

TInt CVersificatorContainer::CountComponentControls() const
{
	return 0;
}

CCoeControl* CVersificatorContainer::ComponentControl(TInt /*aIndex*/) const
{
	return NULL;
}

void CVersificatorContainer::SetTitle(const TDesC& aTitle)
{
	DELETE(iTitle);
	iTitle = aTitle.Alloc();
	iTitleWidth = iTitleFont->TextWidthInPixels(aTitle);
}

void CVersificatorContainer::SetColors(TRgb aBackBorderColor, TRgb aBackShadowColor, TRgb aBackColor, TRgb aInsideBorderColor, TRgb aInsideColor, TRgb aForeColor)
{
	iBackBorderColor = aBackBorderColor;
	iBackShadowColor = aBackShadowColor;
	iBackColor = aBackColor;
	iInsideBorderColor = aInsideBorderColor;
	iInsideColor = aInsideColor;
	iForeColor = aForeColor;
}

void CVersificatorContainer::SetTickerText(const TDesC& aText)
{
	//Set text
	iTicker->SetText(aText, iTickerWidth);
}

void CVersificatorContainer::SetTickerTextFormat(TRefByValue<const TDesC16> aFmt, ...)
{
	//Set text
	iTicker->SetTextFormat(aFmt, iTickerWidth);
}

void CVersificatorContainer::SetTickerIcon(CFbsBitmap* aIconBitmap, CFbsBitmap* aIconBitmapMask)
{
	iIconBitmap = aIconBitmap;
	iIconBitmapMask = aIconBitmapMask;

	//Repaint
	Repaint();
}

void CVersificatorContainer::Repaint()
{
	TTime now;
	now.HomeTime();

	TTimeIntervalMicroSeconds msecs = now.MicroSecondsFrom(iLastDrawTime);
	TUint diff = (msecs.Int64()/1000).Low();

	if (diff < MICRO_SECONDS_MAX_REPAINT) return;
	iLastDrawTime.HomeTime();
	
	DrawDeferred();
}

void CVersificatorContainer::Draw(const TRect& aRect) const
{
	CWindowGc& gc = SystemGc();
	gc.Clear(aRect);

	//Border line
	TRect r(aRect.Size());
	gc.SetPenColor(iBackBorderColor);
	gc.DrawRect(r);

	//Shadow line
	r.iTl.iX++;
	r.iTl.iY++;

	r.iBr.iX--;
	r.iBr.iY--;

	gc.SetPenColor(iBackShadowColor);
	gc.SetBrushColor(iBackColor);
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.DrawRect(r);

	//Header
	r.iTl.iX += 3;
	r.iTl.iY += 3;

	r.iBr.iX -= 4;
	r.iBr.iY = r.iTl.iY + iTitleFontHeight + 9;

	//Set title rect
	TRect titleRect = r;
	titleRect.iTl.iX += 3;
	titleRect.iTl.iY += 3;
	titleRect.iBr.iX -= 2;
	titleRect.iBr.iY -= 2;

	//Draw header
	gc.SetPenColor(iInsideBorderColor);
	gc.SetPenSize(TSize(2, 2));
	gc.SetBrushColor(iInsideColor);
	gc.DrawRoundRect(r, TSize(4, 4));

	//Set main region rect
	TRect mainRegion;
	mainRegion.iTl.iX = r.iTl.iX;
	mainRegion.iTl.iY = r.iBr.iY + 4;
	mainRegion.iBr.iX = r.iBr.iX;

	r.iBr.iY = aRect.iBr.iY - 5;
	r.iTl.iY = r.iBr.iY - iTextFontHeight - 8;

	//Set footer rect
	TRect footerRegion = r;
	footerRegion.iTl.iX += 3;
	footerRegion.iTl.iY += 3;
	footerRegion.iBr.iX -= 3;
	footerRegion.iBr.iY -= 3;
	
	//Draw footer
	gc.DrawRoundRect(r, TSize(4, 4));

	//Draw main region
	mainRegion.iBr.iY = r.iTl.iY - 4;
	gc.DrawRoundRect(mainRegion, TSize(4, 4));

	//Draw title
	gc.SetPenSize(TSize(1, 1));
	gc.SetClippingRect(titleRect);
	gc.UseFont(iTitleFont);

	TPoint p((titleRect.iBr.iX + titleRect.iTl.iX - iTitleWidth) >> 1, titleRect.iBr.iY - 5);

	if (iTitle)
	{
		//Draw title border
		gc.SetPenColor(iInsideBorderColor);
		gc.DrawText(iTitle->Des(), TPoint(p.iX, p.iY + 1));
		gc.DrawText(iTitle->Des(), TPoint(p.iX, p.iY - 1));
		gc.DrawText(iTitle->Des(), TPoint(p.iX + 1, p.iY));
		gc.DrawText(iTitle->Des(), TPoint(p.iX - 1, p.iY));

		//Draw text
		gc.SetPenColor(iBackShadowColor);
		gc.DrawText(iTitle->Des(), p);
	}

	//Draw ticker
	gc.CancelClippingRect();
	gc.SetClippingRect(footerRegion);
	gc.UseFont(iTextFont);

	//Draw ticker text
	TRect iconRect(0, 0, 10, 10);
	if (iTicker && iTicker->Length() > 0)
	{
		TInt baseline = ((footerRegion.Height() + iTextFont->AscentInPixels()) >> 1) - 1; 
		gc.SetPenColor(iForeColor);
		gc.DrawText(iTicker->GetText(), footerRegion, baseline, CGraphicsContext::ELeft, footerRegion.iTl.iX + iconRect.iBr.iX + 1);
	}

	//Draw ticker icon
	if (iIconBitmap)
	{
		TPoint iconOrigin(footerRegion.iTl.iX + 1, (footerRegion.iBr.iY + footerRegion.iTl.iY - iconRect.Height()) >> 1);
		gc.BitBltMasked(iconOrigin, iIconBitmap, iconRect, iIconBitmapMask, EFalse);
	}

	//Reduce main region
	mainRegion.iTl.iX += 3;
	mainRegion.iTl.iY += 3;
	mainRegion.iBr.iX -= 2;
	mainRegion.iBr.iY -= 2;

	//Set main region as clipping area
	gc.CancelClippingRect();
	gc.SetClippingRect(mainRegion);

	//Delegates drawing to current plugin in main region
	if (iAppUi.iCurrentPlugin) iAppUi.iCurrentPlugin->Draw(gc, mainRegion);

	gc.CancelClippingRect();
}
